package com.aysafety.iotdataplatformweb.services.microseism.impl;

import com.aysafety.core.common.DateUtils;
import com.aysafety.iotdataplatformweb.dao.MicroseismDao;
import com.aysafety.iotdataplatformweb.services.microseism.MicroseismService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service public class MicroseismServiceImpl implements MicroseismService {

    private final Map<String, Object> warning = new HashMap<String, Object>() {
        {
            put("entId", "String");
            put("w1", "Double");
            put("fitness", "Double");
            put("datetime", "Date");
            put("last", "String");
        }
    };

    @Autowired MicroseismDao microseismDao;


    @Override
    public long countByMicroseism(String mineId, String startTime, String endTime, int timeSpan, String timeUnit)
        throws Throwable {
        long startTimeTimestamp = DateUtils.fomatDateTime(startTime).getTime();
        long endTimeTimestamp = DateUtils.fomatDateTime(endTime).getTime();
        Map<String, Object> parram = new HashMap<String, Object>();
        parram.put("tableId", mineId + "." + "MicroseismRealTime");
        parram.put("entId", "\'" + mineId + "\'");
        parram.put("startTime", "\'" + startTimeTimestamp + "\'");
        parram.put("endTime", "\'" + endTimeTimestamp + "\'");
        return microseismDao.countRealMicroseism(parram);
    }

    /**
     * @Description: 实时
     * @Param: [mineId, startTime, endTime, timeSpan, timeUnit, page, size, startRow]
     * @return: java.util.List<java.util.Map               <               java.lang.String               ,               java.lang.Object>>
     * @Author: ChengQi
     * @Date: 2019/1/29
     */
    @Override public List<Map<String, Object>> queryRealTimeByMicroseism(String mineId, String startTime,
        String endTime, int timeSpan, String timeUnit, int page, int size, String startRow) throws Throwable {
        long startTimeTimestamp = DateUtils.fomatDateTime(startTime).getTime();
        long endTimeTimestamp = DateUtils.fomatDateTime(endTime).getTime();
        Map<String, Object> parram = new HashMap<String, Object>();
        parram.put("tableId", mineId + "." + "MicroseismRealTime");
        parram.put("entId", "\'" + mineId + "\'");
        parram.put("startTime", "\'" + startTimeTimestamp + "\'");
        parram.put("endTime", "\'" + endTimeTimestamp + "\'");
        List<Map<String, Object>> result = microseismDao.queryRealMicroseism(parram);
        for (int i = 0; i < result.size(); i++) {
            long timestamp = (long) result.get(i).get("datetime");
            Date dateTime = DateUtils.fomatDateTime(timestamp);
            result.get(i).put("datetime", DateUtils.getTime(dateTime));
        }
        return result;
    }

    /**
     * @Description: 微震大能量数据
     * @Param: [mineId, startTime, endTime]
     * @return: java.util.List<java.util.Map                               <                               java.lang.String                               ,                               java.lang.Object>>
     * @Author: ChengQi
     * @Date: 2018/12/26
     */
    @Override public List<Map<String, Object>> queryWeblargeEnergy(String mineId, String startTime, String endTime)
        throws Throwable {

        long startTimeTimestamp = DateUtils.fomatDateTime(startTime).getTime();
        long endTimeTimestamp = DateUtils.fomatDateTime(endTime).getTime();
        Map<String, Object> parram = new HashMap<String, Object>();

        parram.put("tableId", "sysParameter");
        parram.put("pk", "\'" + mineId + ".Microseism" + "\'");
        Map<String, Object> sysParameter = microseismDao.querySys(parram);

        parram.put("tableId", mineId + "." + "MicroseismRealTime");
        parram.put("entId", "\'" + mineId + "\'");
        parram.put("startTime", "\'" + startTimeTimestamp + "\'");
        parram.put("endTime", "\'" + endTimeTimestamp + "\'");
        parram.put("emergoa", sysParameter.get("LARGEENERGY"));

        List<Map<String, Object>> result = microseismDao.queryRealMicroseism(parram);
        for (int i = 0; i < result.size(); i++) {
            long timestamp = (long) result.get(i).get("datetime");
            Date dateTime = DateUtils.fomatDateTime(timestamp);
            result.get(i).put("datetime", DateUtils.getTime(dateTime));
        }
        return result;
    }

    /**
     * @Description: 频次
     * @Param: [mineId, startTime, endTime]
     * @return: java.util.List<java.util.Map               <               java.lang.String               ,               java.lang.Object>>
     * @Author: ChengQi
     * @Date: 2018/12/26
     */
    @Override public List<Map<String, Object>> queryWebFrequency(String mineId, String startTime, String endTime,
        int size) throws Throwable {
        long startTimeTimestamp = DateUtils.fomatDateTime(startTime).getTime();
        long endTimeTimestamp = DateUtils.fomatDateTime(endTime).getTime();
        Map<String, Object> parram = new HashMap<String, Object>();
        parram.put("tableId", mineId + "." + "CalFrequency");
        parram.put("entId", "\'" + mineId + "\'");
        parram.put("startTime", "\'" + startTimeTimestamp + "\'");
        parram.put("endTime", "\'" + endTimeTimestamp + "\'");
        //parram.put("size", size);
        List<Map<String, Object>> result = microseismDao.queryFrequency(parram);
        for (int i = 0; i < result.size(); i++) {
            long timestamp = (long) result.get(i).get("datetime");
            Date dateTime = DateUtils.fomatDateTime(timestamp);
            result.get(i).put("datetime", DateUtils.getTime(dateTime));
        }
        return result;
    }

    @Override public long countWebFrequency(String mineId, String startTime, String endTime) throws Throwable {
        long startTimeTimestamp = DateUtils.fomatDateTime(startTime).getTime();
        long endTimeTimestamp = DateUtils.fomatDateTime(endTime).getTime();
        Map<String, Object> parram = new HashMap<String, Object>();
        parram.put("tableId", mineId + "." + "CalFrequency");
        parram.put("entId", "\'" + mineId + "\'");
        parram.put("startTime", "\'" + startTimeTimestamp + "\'");
        parram.put("endTime", "\'" + endTimeTimestamp + "\'");
        return microseismDao.countFrequency(parram);
    }

    /**
     * @Description: 能量偏差值
     * @Param: [mineId, startTime, endTime]
     * @return: java.util.List<java.util.Map   <   java.lang.String   ,   java.lang.Object>>
     * @Author: ChengQi
     * @Date: 2019/3/13
     */
    @Override public List<Map<String, Object>> queryWebEnergyDeviation(String mineId, String startTime, String endTime)
        throws Throwable {

        long startTimeTimestamp = DateUtils.fomatDateTime(startTime).getTime();
        long endTimeTimestamp = DateUtils.fomatDateTime(endTime).getTime();
        Map<String, Object> parram = new HashMap<String, Object>();
        parram.put("tableId", mineId + "." + "CalMicroseism");
        parram.put("entId", "\'" + mineId + "\'");
        parram.put("startTime", "\'" + startTimeTimestamp + "\'");
        parram.put("endTime", "\'" + endTimeTimestamp + "\'");

        List<Map<String, Object>> result = microseismDao.queryEnergyDeviation(parram);
        for (int i = 0; i < result.size(); i++) {
            long timestamp = (long) result.get(i).get("datetime");
            Date dateTime = DateUtils.fomatDateTime(timestamp);
            result.get(i).put("datetime", DateUtils.getTime(dateTime));
        }

        return result;
    }


    @Override public List<Map<String, Object>> frequencyPer(String mineId, String startTime, String endTime)
        throws Throwable {
        long startTimeTimestamp = DateUtils.fomatDateTime(startTime).getTime();
        long endTimeTimestamp = DateUtils.fomatDateTime(endTime).getTime();
        Map<String, Object> parram = new HashMap<String, Object>();
        parram.put("tableId", mineId + "." + "CalMicroseism");
        parram.put("entId", "\'" + mineId + "\'");
        parram.put("startTime", "\'" + startTimeTimestamp + "\'");
        parram.put("endTime", "\'" + endTimeTimestamp + "\'");

        List<Map<String, Object>> result = microseismDao.queryEnergyDeviation(parram);
        for (int i = 0; i < result.size(); i++) {
            long timestamp = (long) result.get(i).get("datetime");
            Date dateTime = DateUtils.fomatDateTime(timestamp);
            result.get(i).put("datetime", DateUtils.getTime(dateTime));
        }

        return result;
    }

    @Override public Map<String, Object> comprehensiveWarning(String mineId, String startTime, String endTime)
        throws Throwable {
        Map<String, Object> resultMap = new HashMap<>();
        long startTimeTimestamp = DateUtils.fomatDateTime(startTime).getTime();
        long endTimeTimestamp = DateUtils.fomatDateTime(endTime).getTime();
        Map<String, Object> parram = new HashMap<String, Object>();
        parram.put("tableId", "MicroseismWarningStudy");
        parram.put("pk", "\'" + mineId + "\'");
        Map<String, Object> studyInfo = microseismDao.queryMicroseismWarningStudy(parram);
        //double R1 = Double.parseDouble(studyInfo.get("R1").toString());
        double R2 = Double.parseDouble(studyInfo.get("R2").toString());
        //Map<String, Object> map = new HashMap<String, Object>();
        //map.put("R1", R1 > R2 ? R2 : R1);//短预警
        //map.put("R2", R1 > R2 ? R1 : R2);//长预警
        resultMap.put("R",R2);
        parram.put("tableId", mineId + "." + "MicroseismWarning");
        parram.put("size", 1);
        List<Map<String, Object>> w = microseismDao.queryWarning(parram);
        parram.put("startTime", "\'" + startTimeTimestamp + "\'");
        parram.put("endTime", "\'" + endTimeTimestamp + "\'");
        parram.remove("size");
        List<Map<String, Object>> wLists = microseismDao.queryWarning(parram);
        for (int i = 0; i < wLists.size(); i++) {
            long timestamp = (long) wLists.get(i).get("DATETIME");
            Date dateTime = DateUtils.fomatDateTime(timestamp);
            wLists.get(i).put("DATETIME", DateUtils.getTime(dateTime));
        }
        resultMap.put("W",w.get(0));
        resultMap.put("wLists",wLists);
        return resultMap;
    }

    /**
     * @Description: 离散度
     * @Param: [mineId, startTime, endTime]
     * @return: java.util.List<java.util.Map   <   java.lang.String   ,   java.lang.Object>>
     * @Author: ChengQi
     * @Date: 2019/3/13
     */
    @Override public List<Map<String, Object>> queryDispersion(String mineId, String startTime, String endTime) {
        long startTimeTimestamp = DateUtils.fomatDateTime(startTime).getTime();
        long endTimeTimestamp = DateUtils.fomatDateTime(endTime).getTime();
        Map<String, Object> parram = new HashMap<String, Object>();
        parram.put("tableId", mineId + "." + "calDispersion");
        parram.put("entId", "\'" + mineId + "\'");
        parram.put("startTime", "\'" + startTimeTimestamp + "\'");
        parram.put("endTime", "\'" + endTimeTimestamp + "\'");

        List<Map<String, Object>> result = microseismDao.queryDispersion(parram);
        for (int i = 0; i < result.size(); i++) {
            long timestamp = (long) result.get(i).get("datetime");
            Date dateTime = DateUtils.fomatDateTime(timestamp);
            result.get(i).put("datetime", DateUtils.getTime(dateTime));
        }

        return result;
    }
}
